/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAL;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import object.CD;

/**
 *
 * @author anhvh_b00319
 */
public class DABasic {

    private static Connection conn;

    private static Connection getConnection() throws Exception {
        Class.forName(com.microsoft.sqlserver.jdbc.SQLServerDriver.class.getName());
        Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=cdcatalog", "sa", "fat123456");
        return con;
    }

    public static void insertCD(CD cd) throws Exception {
        if (conn == null) {
            conn = getConnection();
        } else if (conn.isClosed()) {
            conn = getConnection();
        }

        PreparedStatement ps = conn.prepareStatement(
                "INSERT INTO dbo.cd (title, artist, country, company, price, year) "
                + "VALUES ( ? , ? , ? , ? , ? , ? )");

        ps.setString(1, cd.getTitle());
        ps.setString(2, cd.getArtist());
        ps.setString(3, cd.getCountry());
        ps.setString(4, cd.getCompany());
        ps.setDouble(5, cd.getPrice());
        ps.setString(6, cd.getYear());

        ps.executeUpdate();
    }

    public static void closeCon() throws SQLException {
        if (conn != null) {
            if (!conn.isClosed()) {
                conn.close();
            }
        }
    }
}
